package cn.wizzer.app.web.modules.controllers.user;

import cn.wizzer.app.user.modules.services.UserSignService;
import cn.wizzer.app.web.commons.filter.ApiTokenFilter;
import cn.wizzer.framework.util.Result;
import cn.wizzer.framework.util.param.ParamVerify;
import com.alibaba.dubbo.config.annotation.Reference;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.annotation.*;

import java.math.BigDecimal;

@IocBean
@At("/api/user/sign")
public class ApiUserSignController {

    @Inject
    @Reference
    private UserSignService userSignService;

    /**
     * 签到
     *
     * @return Object
     */
    @At("/sign")
    @Ok("json")
    @Filters(@By(type = ApiTokenFilter.class))
    public Object sign() {
        String appid = Mvcs.getReq().getHeader("appid");
        userSignService.sign(appid);
        return Result.success();
    }

    /**
     * 兑换金币
     *
     * @param amount
     * @return Object
     */
    @At("/exchange")
    @Ok("json")
    @Filters(@By(type = ApiTokenFilter.class))
    public Object exchange(@ParamVerify(required = true, name = "amount") String amount) {
        String appid = Mvcs.getReq().getHeader("appid");
        userSignService.exchange(appid, new BigDecimal(amount));
        return Result.success();
    }


    /**
     * 查询用户自己的金币账户及签到情况
     *
     * @return Object
     */
    @At("/querySign")
    @Ok("json")
    @Filters(@By(type = ApiTokenFilter.class))
    public Object querySign() {
        String appid = Mvcs.getReq().getHeader("appid");
        return Result.success(userSignService.querySign(appid));
    }

    /**
     * 分页查询金币记录
     *
     * @return Object
     */
    @At("/pageList")
    @Ok("json")
    @Filters(@By(type = ApiTokenFilter.class))
    public Object pageList(@ParamVerify(required = true, name = "pageNumber") int pageNumber,
                           @ParamVerify(required = true, name = "pageSize") int pageSize) {
        String appid = Mvcs.getReq().getHeader("appid");
        return Result.success(userSignService.pageList(appid, pageNumber, pageSize));
    }


}
